<?php
require_once('../../core.inc.php');
require_once('Model/SiteManage.php');
require_once('View/Smarty.php');
require_once('Model/DB.php');
require_once('Model/Validate.php');
require_once('Model/Pager.php');
require_once('Model/Uploader.php');
require_once('Plugin/Site.php');
$sys = new Model_SiteManage();
$sys->checkLogin();
$params = $_REQUEST;
$smarty = new View_Smarty($params);
$site   = new Model_Site();
$smarty->assign('site', $site);

if (isset($params['do']) && function_exists('exec_' . $params['do'])){

		$params['op'] = call_user_func('exec_' . $params['do'], $params, &$smarty, &$site);

}
if (isset($params['op']) && function_exists('op_' . $params['op'])){
    call_user_func('op_' . $params['op'], $params, &$smarty, &$site);
}
else{
    call_user_func('op_search', $params, &$smarty, &$site);
}





$forms = $_POST;
// 検索、一覧
function op_search($params, &$smarty, &$site)
{
    $params['search'] = array();
    $page_id = isset($params['page_id']) && $params['page_id'] > 1 ? $params['page_id'] : 1;
    $max_row = 20;
    $pager   = new Model_Pager();
    $pager->getPagerInfo($site->getListCount($params['search']), $page_id, $max_row);
    $list = $site->getList($params['search'], 'id asc', $max_row, ($max_row * ($page_id - 1)));
    $smarty->assign('list', $list);
    $smarty->assign('pager', $pager);
    $smarty->assign('search', $params['search']);
    $smarty->display('site/site_index.html');
}

// 新規登録
function op_regist($params, &$smarty, &$site)
{
    if (!isset($params['forms'])){
        $forms = array();
        foreach($site->getFormKeys() as $key){
            $forms[$key] = '';
        }
        $forms['plugin'] = array();
        $params['forms'] = $forms;
    }
    else{
        if (!isset($params['forms']['mainte_flg'])){
            $params['forms']['mainte_flg'] = 0;
        }
        if (!isset($params['forms']['plugin'])){
            $params['forms']['plugin'] = array();
        }
    }
    $smarty->assign('phase', 'input');
    $smarty->assign('auth_flag_map', $site->getAuthFlagMap());
    $smarty->assign('mainte_flag_map', $site->getMainteFlagMap());
    $smarty->assign('plugins', __get_config('plugin.ini'));
    $smarty->assign('forms', $params['forms']);
    $smarty->display('site/site_form.html');
}
function exec_validate_regist_form(&$params, &$smarty, &$site)
{
    $err = array();
    if ($site->validate($params['forms'], $err)){
        return exec_regist_form($params, $smarty, $site);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'regist';
}
function exec_regist_form(&$params, &$smarty, &$site)
{
    if(isset($params['forms']['logo_file'])){
        $tmp_logo_file = $params['forms']['logo_file'];
        unset($params['forms']['logo_file']);
    }
    $site_id = $site->insert($params['forms']);
    // logo
    if(isset($tmp_logo_file)){
        $uploader  = new Model_Uploader();
        $cache     = $tmp_logo_file[0];
        $base_name = 'logo';
        $move_to   = $site->getSiteDir($site_id, 1);
        if (!file_exists($move_to. 'images/')){
            mkdir($move_to.'images/');
            chmod($move_to.'images/', 0777);
        }
        $move_to .= 'images/';
        $filename = $uploader->moveCacheImage($cache, $move_to, $base_name);
        $data = array();
        $data['logo_file'] = $filename;
        $site->update($site_id, $data);
    }
    // plugin
    $site->updatePlugin($site_id, $params['forms']['plugin']);
    // tracking
    $site->updateTrackingCode($site_id, $params['forms']);
    //
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=regist_complete&id=' . $site_id);
    exit();
}
function op_regist_complete($params, &$smarty, &$site)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $smarty->assign('forms', array('id' => $params['id']));
    }
    $smarty->assign('message', '登録が完了しました');
    $smarty->display('site/site_form.html');
}

// 編集
function op_edit($params, &$smarty, &$site)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
    if (!isset($params['forms'])){
        $forms = $site->get($id);
        if ($forms['logo_file'] != ''){
            $forms['logo_file'] = array(
                    0 => $site->getSiteUrl($id) . 'images/' . $forms['logo_file']
                    );
        }
        else{
            $forms['logo_file'] = array();
        }
        $forms['plugin'] = $site->getPlugin($id);
        $forms['t_id']   = array();
        $forms['t_name'] = array();
        $forms['t_code'] = array();
        $track = $site->getTrackingCode($id);
        foreach($track as $t){
            $forms['t_id'][]   = $t['id'];
            $forms['t_name'][] = $t['name'];
            $forms['t_code'][] = $t['code'];
        }
        $params['forms'] = $forms;
    }
    else{
        if (!isset($params['forms']['mainte_flg'])){
            $params['forms']['mainte_flg'] = 0;
        }
        if (!isset($params['forms']['plugin'])){
            $params['forms']['plugin'] = array();
        }
    }
    $smarty->assign('id', $id);
    $smarty->assign('phase', 'input');
    $smarty->assign('auth_flag_map', $site->getAuthFlagMap());
    $smarty->assign('mainte_flag_map', $site->getMainteFlagMap());
    $smarty->assign('plugins', __get_config('plugin.ini'));
    $smarty->assign('forms', $params['forms']);
    $smarty->display('site/site_form.html');
}


// 編集email
function op_editemail($params, &$smarty, &$site)
{
	$status = $params['emailmap'];
	if($status==""){

		$status="user_insert";
	}

    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
    if (!isset($params['forms'])){
        $forms = $site->getemail($id,$status);
    }

    $smarty->assign('id', $id);
    $smarty->assign('updata', date('Y-m-d H:i:s',time()));
    $smarty->assign('phase', 'input');
    $smarty->assign('emailmap', $site->getmailtemp());
    $smarty->assign('mainte_flag_map', $site->getMainteFlagMap());
    $smarty->assign('plugins', __get_config('plugin.ini'));

    if($params['forms']==""){
    	$smarty->assign('forms', $forms);
    }else{
    	$params['forms']['u_time'] = $params['u_time'];
    	$params['forms']['etitle'] = $params['etitle'];
    	$smarty->assign('forms', $params['forms']);
    }

    $smarty->display('site/site_email.html');

}
function exec_validate_editemail_form($params, &$smarty, &$site)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $err = array();
    if ($site->validateEmail($params['forms'], $err)){
       return exec_editemail_form($params, $smarty, $site);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'editemail';
}
function exec_validate_edit_form(&$params, &$smarty, &$site)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    if ($params['mode'] == 'delete')
    {
        return exec_delete_form($params, $smarty, $site);
    }
    $err = array();
    if ($site->validate($params['forms'], $err)){
        return exec_edit_form($params, $smarty, $site);
    }
    $validate = new Model_Validate();
    $smarty->assign('validate', $validate);
    $smarty->assign('err', $err);
    return 'edit';
}
function exec_editemail_form(&$params, &$smarty, &$site)
{

    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
    $emailmap = $params['emailmap'];
	$site_id = $id;
    $dir = "../../templates/mail/".$site_id;
    $str=	$site->updateEmail($site_id, $params['forms'],$emailmap,$dir);

     create_folders($dir);
     $filename=  $dir."/".$emailmap.".txt";




     if(file_exists($filename))
    {
     unlink($filename);
    }
    create_txt($filename,$params['forms']);




	header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=edit_complete&id='. $id);
    exit();
}


function create_folders($dir){

       return is_dir($dir) or (create_folders(dirname($dir)) and mkdir($dir, 0777));

     }
function create_txt($filename,$data){
$fp=fopen($filename,"w+");//fopen()的其它???参看相?函数
$str=$data['letter_content'];
fputs($fp,$str);
fclose($fp);

 }
function exec_edit_form(&$params, &$smarty, &$site)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $id = $params['id'];
	if(isset($params['forms']['logo_file'])){
	    $tmp_logo_file = $params['forms']['logo_file'];
	    unset($params['forms']['logo_file']);
	}
	$site_id = $id;
	$site->update($site_id, $params['forms']);
	// logo
	if(isset($tmp_logo_file)){
	    $uploader  = new Model_Uploader();
	    $cache     = $tmp_logo_file[0];
	    if (!preg_match('/^http:\/\//', $cache)){
    	    $base_name = 'logo';
    	    $move_to   = $site->getSiteDir($site_id, 1);
    	    if (!file_exists($move_to. 'images/')){
    	        mkdir($move_to.'images/');
    	        chmod($move_to.'images/', 0777);
    	    }
    	    $move_to .= 'images/';
    	    $filename = $uploader->moveCacheImage($cache, $move_to, $base_name);
    	    $data = array();
    	    $data['logo_file'] = $filename;
    	    $site->update($site_id, $data);
	    }
	}
	// plugin
	$site->updatePlugin($site_id, $params['forms']['plugin']);
	// tracking
	$site->updateTrackingCode($site_id, $params['forms']);

	header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=edit_complete&id=' . $id);
    exit();
}
function op_edit_complete($params, &$smarty, &$site)
{
    $smarty->assign('phase', 'complete');
    if(isset($params['id'])){
        $smarty->assign('id', $params['id']);
        $smarty->assign('forms', array('id' => $params['id']));
    }
    $smarty->assign('message', '更新が完了しました');
    $smarty->display('site/site_form.html');
}
function exec_delete_form(&$params, &$smarty, &$site)
{
    if (!isset($params['id'])){
        __raise_error();
    }
    $site->deleteSite($params['id']);
    header('Location: '. $_SERVER['SCRIPT_NAME'] . '?op=delete_complete&id=' . $id);
    exit();
}
function op_delete_complete($params, &$smarty, &$site)
{
    $smarty->assign('phase', 'complete');
    $smarty->assign('message', '削除が完了しました');
    $smarty->display('site/site_form.html');
}



?>